﻿using System;
using System.Data;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

using ent;

public partial class main_search_results : System.Web.UI.Page {
    public String URL = ApplicationCommon.getPath();
    public String URL_CONTENT = "~/main/content.aspx";

    private String _qsSearch = String.Empty;

    private bool _permitManagement = false;

    private TableContents t2 = new TableContents();
    private TableLists t3 = new TableLists();

    protected void Page_Load(Object sender, EventArgs e) {
        _qsSearch = Request.QueryString["q"];

        // Authenticate.
        authenticateUser();
        // Search.
        if(!ApplicationCommon.isEmpty(_qsSearch)) {
            Hashtable p1 = new Hashtable();
            p1.Add("search", _qsSearch);
            rptrSearchResults.DataSource = t2.getContentsBySearch(p1);
            rptrSearchResults.DataBind();
        }
        // Is empty.
        if(rptrSearchResults.Items.Count <= 0) {
            lblError.Text = ResourceCommon.msgError_NoRecordsFound;
        }
    }

#region Methods

    /// <summary>Authenticate user.</summary>
    protected void authenticateUser() {
        if(!ApplicationCommon.isValidSession(Session)) {
            _permitManagement = false;
        } else {
            _permitManagement = true;
        }
    }

#endregion

#region Events

    protected void rptrSearchResults_ItemDataBound(Object sender, RepeaterItemEventArgs e) {
        // This event is raised for the header, the footer, separators, and items.
        if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {
            DataRowView drv1 = (DataRowView)e.Item.DataItem;

            // Set header.
            HyperLink hypPostHeader = ((HyperLink)e.Item.FindControl("hypItemTitle"));
            Hashtable qs1 = new Hashtable();
            qs1.Add("id", drv1[TableContents.TBL__contents__content_id].ToString());
            hypPostHeader.NavigateUrl = ApplicationCommon.setQueryString(qs1, URL_CONTENT);
            hypPostHeader.Text = (String)drv1[TableContents.TBL__contents__content_title];
            
            // Set status.
            System.Text.StringBuilder sb2 = new System.Text.StringBuilder(); 
            if(_permitManagement) {
                sb2.AppendFormat("Status: <span>{0} {1}</span>", drv1[TableContents.TBL__contents__content_status].ToString(), t3.getItemName((int)TableLists.enumList.ContentStatuses, (int)drv1[TableContents.TBL__contents__content_status]));
                sb2.Append(" &bull; ");
                sb2.AppendFormat("Created: <span>{0}</span>", ((DateTime)drv1[TableContents.TBL__contents__content_date_created]).ToShortDateString());
                sb2.Append(" &bull; ");
            }
            if(!(drv1[TableContents.TBL__contents__content_date_published] is DBNull)) {
                sb2.AppendFormat("Published: <span>{0}</span>", ((DateTime)drv1[TableContents.TBL__contents__content_date_published]).ToShortDateString());
            } else {
                sb2.AppendFormat("Published: <span>{0}</span>", "None");
            }
            sb2.Append(" &bull; ");
            sb2.AppendFormat("Modified: <span>{0}</span>", ((DateTime)drv1[TableContents.TBL__contents__content_date_modified]).ToString());
            ((Literal)e.Item.FindControl("litItemStatus")).Text = sb2.ToString();
            // Set body.
            ((Literal)e.Item.FindControl("litItemBody")).Text = new System.Text.StringBuilder().AppendFormat("<p>{0}</p>", ApplicationCommon.stripHtml(ApplicationCommon.shortString(HttpUtility.HtmlDecode((String)drv1[TableContents.TBL__contents__content_body]), 512))).ToString();
        }
    }
    protected void rptrSearchResults_ItemCommand(Object sender, RepeaterCommandEventArgs e) { }

#endregion

}
